package com.hs.android.fortress.ingame;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.hs.android.fortress.R;
import com.hs.android.fortress.ingame.GameInformationGetter.Listener;
import com.hs.android.fortress.model.ExplosionInformation;
import com.hs.android.fortress.model.Player;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public abstract class AbstractGameActivity extends Activity{
	
	private static final String TAG = AbstractGameActivity.class.getSimpleName();
	
	protected AbstractMainScreenFragment mMainScreenFragment;
	protected AbstractMenuScreenFragment mMenuScreenFragment;
	
	protected GameInformationGetter mGetter;
	private Timer mTimer;
	
	protected String mUserId;
	protected int mRoomNumber;
	
	protected Listener mGetterListener;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gamescreen);
		
		mUserId = setUserId();
		mRoomNumber = setRoomNumber();
		
		FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		
		mMainScreenFragment = (AbstractMainScreenFragment)fm.findFragmentById(R.id.mainscreen_frag);
		if(mMainScreenFragment == null){
			mMainScreenFragment = createMainScreenFragment();
			ft.add(R.id.mainscreen_frag, mMainScreenFragment);
		}
		
		mMenuScreenFragment = (AbstractMenuScreenFragment)fm.findFragmentById(R.id.menuscreen_frag);
		if(mMenuScreenFragment == null){
			mMenuScreenFragment = createMenuScreenFragment();
			ft.add(R.id.menuscreen_frag, mMenuScreenFragment);
		}
		
		ft.commit();
		
		mGetter = new GameInformationGetter(new Handler());
		mGetter.start();
		mGetter.getLooper();
	}
	
	protected abstract String setUserId();
	protected abstract int setRoomNumber();
	
	protected abstract AbstractMainScreenFragment createMainScreenFragment();
	protected abstract AbstractMenuScreenFragment createMenuScreenFragment();

	protected abstract void setPlayer(Player myPlayer);
	
	protected void setGetterListener(Listener getterListener){
		mGetter.setListener(getterListener);
	}
	
	@Override
	protected void onResume() {
		super.onResume();

		TimerTask task = new TimerTask() {

			@Override
			public void run() {
				mGetter.queueRoomNumber(mRoomNumber);
			}
		};

		mTimer = new Timer();
		mTimer.schedule(task, 0, 1000);
	};
	
	@Override
	protected void onPause() {
		super.onPause();

		if (mTimer != null) {
			mTimer.cancel();
			mTimer.purge();
			mTimer = null;
		}
	};
	
	
	protected void onDestroy() {
		super.onDestroy();

		mGetter.clearQueue();
		mGetter.quit();
	};
	
	protected final void DrawEnermyExplostion(List<ExplosionInformation> explosionInformations){
		
		for(ExplosionInformation ex : explosionInformations){
			if(!mUserId.equals(ex.getShooter())){
				mMainScreenFragment.drawEnermyExplosion(ex.getPostion(), ex.getBomb());
			}
		}
		
	}
	
	protected final void setGottenInformation(List<Player> players){
		Player myPlayer = null;
		
		if(players != null){
			
			for(Player player : players){
				if(mUserId.equals(player.getId()))
					myPlayer = player;
			}
			
			if(myPlayer != null){
				setPlayer(myPlayer);
			}
			
			if(players.size() == 0){
				draw();
			}else if(players.size() == 1 && myPlayer != null){
				confirmWinning();
			}else if(myPlayer == null || myPlayer.getEnergy() <= 0){
				Lose();
			}else{
				
				mMainScreenFragment.setPlayer(players);
				
				mMainScreenFragment.drawMyLocation();
				mMainScreenFragment.setUpMapMode();
				mMainScreenFragment.drawEnermyLocation();
				mMainScreenFragment.setEnermyProgress();
				
				
				mMenuScreenFragment.setMyLocation(myPlayer.getPosition());
				mMenuScreenFragment.setMyEnergyBar(myPlayer);
			}
			
		}
		
		mMainScreenFragment.setPlayer(players);
	}
	
	protected abstract void confirmWinning();
	protected abstract void Lose();
	protected abstract void draw();
}